<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');	
	require_once('time.inc');
	
	session_start();

    check_permissions(CT_PERM_COMENZI);
	html_header('LANG_Show_comenzi', null, "show_comenzi");
    
	$linkuri = array(LANG_Home => "home.php", LANG_Comenzi => "");	
	titlu_pagina($linkuri);

	$db = db_connect();
	
	$id_client = $_SESSION['user_logat']->id_client;
    $user = $_SESSION['user_logat'];
	
	try {		
		$comenzi = Comanda::queryLoadByIdClient($db, $id_client);
	}
	catch (QueryLoadComandaException $e) {
		redirectOnError(CT_SHOW_COMENZI_LOAD_COMANDA);
	}
	catch (QueryLoadAddressException $e) {
		redirectOnError(CT_SHOW_COMENZI_LOAD_ADDRES);
	}
	catch (QueryLoadPlatiComandaException $e) {
		redirectOnError(CT_SHOW_COMENZI_LOAD_PLATI_COMANDA);
	}
	
	$total = count($comenzi);
	
	$nr_pagini = ceil($total / $user->comenzi_per_pag);
    output_error_msg();
    output_notice_msg();
    output_extra_text(LANG_EXTRA_VIEW_COMENZI);
	
    $coloane_comenzi = explode(",", $user->coloane_comenzi);
	
    echo '<div align="left" class="container">';
	echo "<table align=\"left\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
		echo "<tr>";
			echo "<td valign=\"top\" align=\"left\">";				
				render_filter_and_search_box_comanda($db, $id_client, $_SESSION['user_logat']->comenzi_per_pag);							
			echo "</td>";					
			echo "<td width=\"10\"></td>";
			echo "<td align=\"left\" valign=\"top\" width=\"200\">";
				echo "<table align=\"left\" id=\"wrapper\" width=\"100%\">";
					echo "<thead>";
						echo "<tr>";
							echo "<th align=\"center\">" . LANG_Sume . "</th>";
						echo "</tr>";
					echo "</thead>";
					echo "<tbody>";
						echo "<tr>";
							echo "<td align=\"left\" valign=\"top\" height=\"100%\" id=\"status_plata\">";
								echo LANG_total_suma_sol . "<br />" . LANG_total_suma_exec;
							echo "</td>";						
						echo "</tr>";
					echo "</tbody>";
				echo "</table>";					
			echo "</td>";
		echo "</tr>";	
	echo "</table>";
	echo "</div>";
	
	echo "<div id=\"pag_and_content\" align=\"left\" class=\"container\">";
		$conditie = "comanda.id_client = $id_client AND comanda.deleted = 0";
		if (isset($_SESSION['comenzi_neincasate'])) {
			$arr = get_comenzi($db, 0, $user->comenzi_per_pag, CT_COMANDA_NEINCASATA_INCASATA_PARTIAL, -1, -1, -1, -1, -1, -1, -1, -1, '', "DESC", $user->coloane_comenzi,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,'data_scadenta_sol', $currencies);
			unset($_SESSION['comenzi_neincasate']);						
		}
		else if (isset($_SESSION['comenzi_neplatite'])) {
			$arr = get_comenzi($db, 0, $user->comenzi_per_pag, -1, CT_COMANDA_NEPLATITA_PLATITA_PARTIAL, -1, -1, -1, -1, -1, -1, -1, '', "DESC", $user->coloane_comenzi,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,'data_scadenta_exec', $currencies);
			unset($_SESSION['comenzi_neplatite']);									
		}
		else $arr = get_comenzi($db, 0, $user->comenzi_per_pag, -1, -1, -1, -1, -1, -1, -1, -1, -1, '', "DESC", $user->coloane_comenzi,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,'data_comenzii', $currencies);
		echo $arr[0];		
	echo "</div>";			
	
	if (isset($_GET['pag'])) $pagina = $_GET['pag'];
?>

<script type="text/javascript">
	set_buton_visible_invisible_show_comenzi();
	introdu_cache_prima_data_comenzi(<?php echo $user->comenzi_per_pag; ?>, <?php echo $id_client; ?>, '', 'ASC', 'pag_and_content');
</script>

<?php html_footer(); ?>